#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ -z "$ROCKETMQ_HOME" ] ; then
  ## resolve links - $0 may be a link to maven's home
  PRG="$0"

  # need this for relative symlinks
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
      PRG="$link"
    else
      PRG="`dirname "$PRG"`/$link"
    fi
  done

  saveddir=`pwd`

  ROCKETMQ_HOME=`dirname "$PRG"`/..

  # make it fully qualified
  ROCKETMQ_HOME=`cd "$ROCKETMQ_HOME" && pwd`

  cd "$saveddir"
fi

export ROCKETMQ_HOME

other_args=" "
enable_proxy=false

while [ $# -gt 0 ]; do
  case $1 in
    --enable-proxy)
      enable_proxy=true
      shift
      ;;
    -c|--configFile)
      broker_config="$2"
      shift
      shift
      ;;
    *)
      other_args=${other_args}" "${1}
      shift
      ;;
  esac
done

if [ "$enable_proxy" = true ]; then
  args_for_proxy=$other_args" -pm local"
  if [ "$broker_config" != "" ]; then
      args_for_proxy=${args_for_proxy}" -bc "${broker_config}
  fi
  sh ${ROCKETMQ_HOME}/bin/runserver-dynamic.sh -Dcom.rocketmq.proxy.configPath=$ROCKETMQ_HOME/conf/rmq-proxy.json -Drocketmq.namesrv.domain=mqcloud-weihubeats.com:80 -Drocketmq.namesrv.domain.subgroup=nsaddr-1  -Drmq.logback.configurationFile=$ROCKETMQ_HOME/conf/rmq.proxy.logback.xml org.apache.rocketmq.proxy.ProxyStartup ${args_for_proxy}
else
  args_for_broker=$other_args
  if [ "$broker_config" != "" ]; then
      args_for_broker=${args_for_broker}" -c "${broker_config}
  fi
  sh ${ROCKETMQ_HOME}/bin/runserver-dynamic.sh -Drmq.logback.configurationFile=$ROCKETMQ_HOME/conf/rmq.broker.logback.xml org.apache.rocketmq.broker.BrokerStartup ${args_for_broker}
fi